<?php
// +----------------------------------------------------------------------
// | RXThinkCMF_EVTP5.1_PRO前后端分离旗舰版框架 [ RXThinkCMF ]
// +----------------------------------------------------------------------
// | 版权所有 2018~2021 南京RXThinkCMF研发中心
// +----------------------------------------------------------------------
// | 官方网站: http://www.rxthink.cn
// +----------------------------------------------------------------------
// | 作者: ZhangXuanNeng <rxthinkcmf@163.com>
// +----------------------------------------------------------------------
// | 免责声明:
// | 本软件框架禁止任何单位和个人用于任何违法、侵害他人合法利益等恶意的行为，禁止用于任何违
// | 反我国法律法规的一切平台研发，任何单位和个人使用本软件框架用于产品研发而产生的任何意外
// | 、疏忽、合约毁坏、诽谤、版权或知识产权侵犯及其造成的损失 (包括但不限于直接、间接、附带
// | 或衍生的损失等)，本团队不承担任何法律责任。本软件框架只能用于公司和个人内部的法律所允
// | 许的合法合规的软件产品研发，详细声明内容请阅读《框架免责声明》附件；
// +----------------------------------------------------------------------

namespace app\admin\service;

use app\admin\model\BoxMemberInfo;
use app\admin\model\RegisterLog;

/**
 * 会员管理-服务类
 * @author ZhangXuanNeng
 * @since 2020/11/20
 * Class MemberService
 * @package app\admin\service
 */
class MemberInfoService extends BaseService
{

    protected $default_avatar_url = 'https://downloads.qn1000a.com/qnbox20240129/ae63ba18c87aa834e996c9e39703cfd4.png';

    /**
     * 构造函数
     * @author ZhangXuanNeng
     * @since 2020/11/20
     * MemberService constructor.
     */
    public function __construct()
    {
        $this->model = new BoxMemberInfo();
    }

    /**
     * 获取数据列表
     * @return array
     */
    public function getList(): array
    {
        $param       = request()->param();
        $map         = [];
        $id          = $param['id'] ?? "";
        $phone       = $param['phone'] ?? "";
        $status      = $param['status'] ?? "";
        $nickname    = $param['nickname'] ?? "";
        $order       = $param['order'] ?? "";
        $sort        = $param['sort'] ?? "";
        $create_time = $param['create_time'] ?? "";
        //  `log_off` tinyint(3) NOT NULL DEFAULT '0' COMMENT '0未注销。  1，申请注销。 2，已注销.',
        //  `age_status` tinyint(1) NOT NULL DEFAULT '0' COMMENT '实名认证状态:0=未实名,1=成年,2=未成年',
        if ($id) {
            $map[] = ['a.id', '=', $id];
        }
        if ($phone) {
            $map[] = ['a.phone', '=', $phone];
        }
        if ($nickname) {
            $map[] = ['a.nickname', '=', $nickname];
        }
        if (!empty($create_time)) {
            $map[] = ['a.create_time', 'between', [strtotime($create_time[0]), strtotime($create_time[1])]];
        }

        switch ($status) {
            case 0:
                $map[] = ['a.log_off', '=', 0];
                break;
            case 1:
                $map[] = ['a.log_off', '=', 1];
                break;
            case 2:
                $map[] = ['a.age_status', 'between', [1, 2]];
                break;
            case 3:
                $map[] = ['a.age_status', '=', 0];
                break;
        }
        $memberModel = (new BoxMemberInfo())->alias('m')
            ->leftJoin('cq_member a', 'a.id = m.member_id')
            ->leftJoin('cq_box_member_account c', 'a.id = c.member_id')
            ->field('a.id as mid,m.id,a.nickname,a.username,a.phone,a.bind_wechat,a.status,a.age_status,a.avatar,m.sex,m.score,m.birthday,c.spend_amount,a.create_time,a.log_off')
            ->where($map);

        if (!empty($order) && !empty($sort)) {
            $orderBy = 'a.' . $sort . ' ' . $order;
        } else {
            $orderBy = 'a.id desc';
        }
        $memberModel = $memberModel->order($orderBy);
        $count       = $memberModel->count();
        $list        = $memberModel->page(PAGE, PERPAGE)->select();
        foreach ($list as &$value) {
            $value['spend_amount'] = $value['spend_amount'] ?? '0.00';
            if (empty($value['avatar'])) {
                $value['avatar'] = $this->default_avatar_url;
            }
        }

        //返回结果
        return array (
            "msg"   => '操作成功',
            "code"  => 0,
            "data"  => $list,
            "count" => $count,
        );
    }


    public function secondTime($seconds = 0)
    {
        $duration = '';

        $seconds = (int)$seconds;
        if ($seconds <= 0) {
            return $duration . '0秒';
        }

        list($day, $hour, $minute, $second) = explode(' ', gmstrftime('%j %H %M %S', $seconds));

        $day -= 1;
        if ($day > 0) {
            $duration .= (int)$day . '天';
        }
        if ($hour > 0) {
            $duration .= (int)$hour . '小时';
        }
        if ($minute > 0) {
            $duration .= (int)$minute . '分钟';
        }
        if ($second > 0) {
            $duration .= (int)$second . '秒';
        }

        return $duration;
    }

    /**
     * 添加或编辑
     * @return mixed
     * @since 2020/11/20
     * @author ZhangXuanNeng
     */
    public function edit()
    {
        // 请求参数
        $data = request()->param();
        // 头像处理
        $avatar = getter($data, 'avatar');
        if (strpos($avatar, "temp")) {
            $data['avatar'] = save_image($avatar, 'member');
        } else {
            $data['avatar'] = str_replace(IMG_URL, "", $data['avatar']);
        }

        // 出生日期
        if ($data['birthday']) {
            $data['birthday'] = strtotime($data['birthday']);
        }

        if ($data['password']) {
            $data['password'] = setMd5Pwd($data['password']);
        } else {
            unset($data['password']);
        }
        return parent::edit($data); // TODO: Change the autogenerated stub
    }

    /**
     * Desc : 设置官方账号
     * User : you name
     * Date : 2024-01-25 10:31
     * @return mixed
     */
    public function editRole()
    {
        $param = request()->param();
        return parent::edit($param['data']);
    }

}
